package com.MyThread;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LuaSetListThread extends Thread {

    private JedisPool jedisPool;

    private int start = 0;

    private int end = 0;

    private String key;

    private String value;

    public LuaSetListThread(JedisPool jedisPool, int start, int end, String key, String value) {
        this.jedisPool = jedisPool;
        this.start = start;
        this.end = end;
        this.key = key;
        this.value = value;
    }

    @Override
    public void run() {
        Jedis jedis = jedisPool.getResource();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS");

        // 清空redis
        jedis.flushDB();

        int num = end - start;
        Date startDate = new Date();
        System.out.println("LuaSetListThread---->" + (num) + ";开始时间：" + sdf.format(startDate));

        String script = "local redisKes = KEYS[1];" +
                "local start = ARGV[1];" +
                "local endd = ARGV[2];" +
                "local value = ARGV[3];" +
                "for i = start, endd-1 do " +
                "    redis.call('lpush',redisKes,value) " +
                "end;" +
                "return 1";

        List<String> keyList = new ArrayList<String>();
        keyList.add(key);

        List<String> argList = new ArrayList<String>();
        argList.add(start + "");
        argList.add(end + "");
        argList.add(value);

        jedis.eval(script,keyList,argList);

        Date endDate = new Date();
        System.out.println("LuaSetListThread---->" + (num) + ";结束时间：" + sdf.format(endDate));

        double qps = (num * 1000) / (endDate.getTime() - startDate.getTime());
        System.out.println("数据结构:String\n操作:Set\n" + "数据量:" + (num) + "\nkey长度:" + key.length()
                + "\nvalue长度:" + value.length() + "\nQPS:" + qps + "/s\n");

        jedis.close();
    }
}
